package Coffee.service;

import Coffee.bean.Coffee;
import Coffee.bean.Result;
import Coffee.dao.CoffeeDao;
import Coffee.dao.CoffeeDaoImpl;

import java.util.List;

public class CoffeeServiceImpl implements CoffeeService{
    CoffeeDao coffeeDao = new CoffeeDaoImpl();
    Result result = new Result();
    @Override
    public Result insertCoffee(Coffee coffee) {
        Coffee c1 = coffeeDao.selectbyname(coffee.getCofname());
        if(c1==null){
            coffeeDao.insertCoffee(coffee);
            result.setMessage("添加成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        result.setMessage("已经有该饮品，添加失败");
        return result;
    }

    @Override
    public int delbyname(String cofname) {
        int i = coffeeDao.delbyname(cofname);
        if(i==1){
            System.out.println("删除成功");
        }else {
            System.out.println("没有该饮品的上架信息");
        }
        return i;
    }

    @Override
    public Coffee selectbyname(String cofname) {
        Coffee selectbyname = coffeeDao.selectbyname(cofname);
        if(selectbyname == null){
            System.out.println("该饮品还未上新");
        }
        return selectbyname;
    }

    @Override
    public int updateCofbyname(String cofname, int cofprice, String state) {
        int i = coffeeDao.updateCofbyname(cofname, cofprice, state);
        if(i==1){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
        return i;
    }

    @Override
    public List<Coffee> selectlikaname(String cofname) {
        return coffeeDao.selectlikaname(cofname);
    }


    @Override
    public List<Coffee> selectAll(String cofName, int pageIndex, int pageSize) {
        return coffeeDao.selectAll(cofName,pageIndex,pageSize);
    }

    @Override
    public int getCount(String cofName) {
        return coffeeDao.getCount(cofName);
    }

    @Override
    public Coffee getCofById(String cofId) {
        return coffeeDao.getCofByid(cofId);
    }

    @Override
    public Result updateCof(Coffee coffee) {
        Result result = new Result();
        int i = coffeeDao.updateCof(coffee);
        if(i>0){
            result.setSuccess(true);
            result.setMessage("修改成功");
            return result;
        }
        result.setSuccess(false);
        result.setMessage("修改失败");
        return result;
    }

    @Override
    public Result delbyId(String id) {
        Result result = new Result();
        int i = coffeeDao.delbyId(id);
        if(i>0){
            result.setSuccess(true);
            result.setMessage("删除成功");
            return result;
        }
        result.setSuccess(false);
        result.setMessage("删除失败");
        return result;
    }

    @Override
    public List<Coffee> getAll() {
        return coffeeDao.getAll();
    }

}
